﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Journal.Common.Extensions;

namespace Journal.Common.Helpers.HtmlControls
{
    public static class InputControls
    {
        public static MvcHtmlString FormInputDropDown<TModel>(this HtmlHelper<TModel> html, IEnumerable<SelectListItem> items)
        {
            TagBuilder select = Select(items);
            return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputDropDown<TModel>(this HtmlHelper<TModel> html, IEnumerable<SelectListItem> items, object htmlAttributes)
        {
            TagBuilder select = Select(items);
            select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputDropDown<TModel>(this HtmlHelper<TModel> html, IEnumerable<SelectListItem> items, IDictionary<string, string> dataParams)
        {
            TagBuilder select = Select(items);
            select = select.InsertDataAttributes(dataParams);
            return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputDropDown<TModel>(this HtmlHelper<TModel> html, IEnumerable<SelectListItem> items, IDictionary<string, string> dataParams, object htmlAttributes)
        {
            TagBuilder select = Select(items);
            select = select.InsertDataAttributes(dataParams);
            select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputPassword<TModel>(this HtmlHelper<TModel> html, object htmlAttributes)
        {
            TagBuilder password = Input("password");
            password.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(password.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputPassword<TModel>(this HtmlHelper<TModel> html)
        {
            TagBuilder password = Input("password");
            return new MvcHtmlString(password.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputSubmit(this HtmlHelper html, string buttonText, object htmlAttributes)
        {
            TagBuilder submitButton = Input("submit");
            submitButton.Attributes.Add("value", buttonText);
            submitButton.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(submitButton.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputSubmit(this HtmlHelper html, string buttonText)
        {
            TagBuilder submitButton = Input("submit");
            submitButton.Attributes.Add("value", buttonText);
            return new MvcHtmlString(submitButton.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputButton(this HtmlHelper html, string buttonText)
        {
            return ButtonControls.Button(buttonText);
        }

        public static MvcHtmlString FormInputButton(this HtmlHelper html, string buttonText, IDictionary<string, string> dataParams)
        {
            return ButtonControls.Button(buttonText, dataParams);
        }

        public static MvcHtmlString FormInputButton(this HtmlHelper html, string buttonText, object htmlAttributes)
        {
            return ButtonControls.Button(buttonText, htmlAttributes);
        }

        public static MvcHtmlString FormInputButton(this HtmlHelper html, string buttonText, object htmlAttributes, IDictionary<string, string> dataParams)
        {
            return ButtonControls.Button(buttonText, htmlAttributes, dataParams);
        }

        private static TagBuilder Select(IEnumerable<SelectListItem> items)
        {
            TagBuilder select = new TagBuilder("select");
            foreach (var item in items)
            {
                TagBuilder option = new TagBuilder("option");
                option.Attributes.Add("value", item.Value);
                option.InnerHtml += item.Text;
                select.InnerHtml += option.ToString(TagRenderMode.Normal);
            }

            return select;
        }

        private static TagBuilder Input(string type)
        {
            TagBuilder input = new TagBuilder("input");
            input.Attributes.Add("type", type);
            return input;
        }
    }
}